package org.jeecg.modules.product.risk.service.impl;

import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.product.risk.entity.TbBaseControlMeasures;
import org.jeecg.modules.product.risk.entity.TbBaseRiskEvents;
import org.jeecg.modules.product.risk.mapper.TbBaseControlMeasuresMapper;
import org.jeecg.modules.product.risk.mapper.TbBaseRiskEventsMapper;
import org.jeecg.modules.product.risk.service.ITbBaseRiskEventsService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;
import java.util.UUID;

/**
 * @Description: 安全风险事件信息
 * @Author: jeecg-boot
 * @Date:   2024-03-02
 * @Version: V1.0
 */
@Service
public class TbBaseRiskEventsServiceImpl extends ServiceImpl<TbBaseRiskEventsMapper, TbBaseRiskEvents> implements ITbBaseRiskEventsService {

	@Autowired
	private TbBaseRiskEventsMapper tbBaseRiskEventsMapper;
	@Autowired
	private TbBaseControlMeasuresMapper tbBaseControlMeasuresMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(TbBaseRiskEvents tbBaseRiskEvents, List<TbBaseControlMeasures> tbBaseControlMeasuresList) {
		tbBaseRiskEvents.setId(UUIDGenerator.generate());
		tbBaseRiskEventsMapper.insert(tbBaseRiskEvents);
		if(tbBaseControlMeasuresList!=null && tbBaseControlMeasuresList.size()>0) {
			for(TbBaseControlMeasures entity:tbBaseControlMeasuresList) {
				//外键设置
				entity.setRiskEventId(tbBaseRiskEvents.getId());
				entity.setId(UUIDGenerator.generate());
				tbBaseControlMeasuresMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(TbBaseRiskEvents tbBaseRiskEvents,List<TbBaseControlMeasures> tbBaseControlMeasuresList) {
		tbBaseRiskEventsMapper.updateById(tbBaseRiskEvents);

		/*这里修改的时候不做物理删除，只做普通修改*/
		//1.先删除子表数据
		//tbBaseControlMeasuresMapper.deleteByMainId(tbBaseRiskEvents.getId());
		
		//2.子表数据重新插入
		if(tbBaseControlMeasuresList!=null && tbBaseControlMeasuresList.size()>0) {
			for(TbBaseControlMeasures entity:tbBaseControlMeasuresList) {
				//外键设置
				if ("".equals(entity.getId()) || null == entity.getId()) {
					entity.setRiskEventId(tbBaseRiskEvents.getId());
					entity.setId(UUIDGenerator.generate());
					tbBaseControlMeasuresMapper.insert(entity);
				}else {
					tbBaseControlMeasuresMapper.updateById(entity);
				}
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		tbBaseControlMeasuresMapper.deleteByMainId(id);
		tbBaseRiskEventsMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			tbBaseControlMeasuresMapper.deleteByMainId(id.toString());
			tbBaseRiskEventsMapper.deleteById(id);
		}
	}
	
}
